package cn.com.idmy.cloud.util;

import cn.com.idmy.base.util.Assert;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

import java.net.URI;


public class CloudUtil {
    @Setter
    private static LoadBalancerClient loadBalancerClient;

    @NotNull
    public static URI getUriByAppKey(@NotNull String appKey) {
        if (loadBalancerClient == null) {
            throw new IllegalStateException("LoadBalancerClient is null");
        }
        var si = loadBalancerClient.choose(appKey);
        Assert.notNull(si, "找不到服务: {}", appKey);
        return si.getUri();
    }
}
